home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7536 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.8 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Specifying a Member Function Address For WNDCLASS Structure
  5. Date: Fri, 23 Feb 1996 15:28:59 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4gkmen$7ja@news.halcyon.com>
  8. References: <4ggu6k$5c1@mother.usf.edu>
  9. NNTP-Posting-Host: blv-pm3-ip26.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12.     Ordinary member functions cannot be callbacks.  There is an implicit
  13. argument to ordinary member functions, the "this" pointer, which
  14. identifies the actual object instance this member function will
  15. operate on, and you can't communicate to Windows that it really needs
  16. to call GameControl::WndProc( &aGameControl, hWnd, uMsg,...).
  17.  
  18.     The solution is to make WndProc a *static* method in the GameControl
  19. class.  Static member functions can be callbacks because they don't
  20. use the implicit "this" pointer.  
  21.  
  22.     Now your concern is sticking the GameControl instance in the window,
  23. which you do by SetWindowLong( GWL_USERDATA, (LPARAM) this ); usually
  24. within the WM_CREATE handling of your wndproc.
  25.  
  26.     Inside your GameControl::WndProc, you can get at the other GameControl
  27. methods and members by This = (GameControl *) GetWindowLong(
  28. GWL_USERDATA ).  Then make your calls like
  29.  
  30.     This->AnotherGameControlMethod( x, y, z )
  31.  
  32.     Lastly, you can either instance GameControl in the WM_CREATE handler
  33. or instance it before the CreateWindow() API and pass the pointer in
  34. its LPARAM argument.
  35.  
  36.     This approach works very well; I've production code in the field using
  37. it.  
  38.  
  39.     Hope this helps.
  40.  
  41.                         --Norm 
  42.  
  43. yatesc@csee.usf.edu (Randy Yates) wrote:
  44.  
  45. >Hello,
  46.  
  47. >I'm writing a MS-Windows 3.1 program and I'd like to have the
  48. >message processing procedure for the window class be a member
  49. >function...
  50.  
  51. >class GameControl
  52. >{
  53. >  private:
  54. >    HWND hWnd; 
  55. >  public:
  56. >    GameControl(HINSTANCE, int);
  57. >    long FAR PASCAL _export WndProc(HWND, UINT, UINT, long);
  58. >};
  59.  
  60. >GameControl::GameControl(HINSTANCE hInstance, int nCmdShow)
  61. >{
  62. >//  static char szAppName[] = "GameControl";
  63. >  WNDCLASS wndclass;
  64.  
  65. >  // Create the window:
  66. >  wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  67. >  wndclass.lpfnWndProc      = &GameControl::WndProc;
  68. >  ...
  69.  
  70. >  hWnd = CreateWindow(
  71. >           PINBULL_CLASSNAME,
  72. >           "PinBull Wizard Game Control",
  73. >           WS_OVERLAPPEDWINDOW,
  74. >           CW_USEDEFAULT,
  75. >           CW_USEDEFAULT,
  76. >           CW_USEDEFAULT,
  77. >           CW_USEDEFAULT,
  78. >           HWND_DESKTOP,
  79. >           NULL,
  80. >           hInstance,
  81. >           NULL);
  82. >   
  83. >  ...
  84.  
  85. >-- 
  86. >% Randy Yates                  % "...the answer lies within your soul
  87. >% EE/Mathematics Student       %       'cause no one knows which side 
  88. >% University of South Florida  %                   the coin will fall."
  89. >% <yatesc@csee.usf.edu>        %  'Big Wheels', *Out of the Blue*, ELO   
  90.  
  91.  
  92.  
  93.